

var MStateObj=cc.Node.extend({
	//枚举值
	EDEBUGFRAME:20,
	EANIM:10,


	curAnim:"",
	nCurState:"",
	curState:null,
	animList:null,
	map:null,
	states:null,//状态数据
	objName:"",


	direction:-1,//物体的面向
	speedx:0,
	speedy:0,
	accSpeedx:0,//加速度
	accSpeedy:0,
	gravity:0.8,
	boundRect:null,

	cmdMgr:null,
	sometest:0,
	country:0,//阵营
	ctor:function(data){
		this._super();
		this.init(data);
	},
	init:function(name){
		this.objName=name;
		this.initState(name);
		this.cmdMgr=new CmdManager();

		//
		var dRt=this.getBoundingRect();
		var uR = cc.LayerColor.create(cc.c4b(0, 0, 255, 180));
        this.addChild(uR,this.EDEBUGFRAME);
        uR.setPosition(dRt.x,0);
        uR.setContentSize(dRt.width,dRt.height);
        uR.setAnchorPoint(0,0);
	},
	initState:function(name){
		this.states=eval(name+"_cns")


		this.animList={};
		for(k in this.states)
		{	
			if(k=="data")
				continue;

			var state=this.states[k];

			var anim=new MAnim(name,state.def.anim);
			this.animList[state.def.anim]=anim;
		}
		this.changeState("0");

	},
	changeAnim:function(index){
		
		if(this.curAnim!="")
		{
			this.removeChild(this.animList[this.curAnim]);
		}

		var anim=this.animList[index];

		var dirs=this.direction;
		anim.setScaleX(dirs);

		anim.resetAnim();
		//anim.scheduleUpdate();
		this.addChild(anim,this.EANIM);
		this.curAnim=index;
	},
	changeState:function(nstate){

		if(nstate==this.nCurState)
		{
			return
		}
		

		
		this.nCurState=nstate;
		this.curState=this.states[nstate];

		//this.resetState();//重置数据
		this.defaultState(nstate);//
		this.changeAnim(this.curState.def.anim);
	},
	defaultState:function(nstate){//改变状态时调用一次
		if(nstate=="41")//跳
		{
			this.speedy=13
		}else if(nstate=="0")
		{
			this.resetSpeed();//重置数据
		}
	},
	resetSpeed:function(){//重置数据
		this.speedx=0;
		this.speedy=0;
	},
	setMap:function(map){
		this.map=map
	},
	updateState:function(dt){
		this.cmdMgr.update(dt);
		this.updateTrigger(dt);

		var dirs=this.direction;
		var anim=this.animList[this.curAnim];
		anim.setScaleX(dirs);
		anim.updateAnim(dt);
		//处理移动
		this.updateMove(dt);
		this.updateClsn(dt);

	},
	updateClsn:function(dt){
		//如果有攻击判定
		var clsnas=this.getClsnas();
		if(clsnas){
			g_fightUtil.Attack(this,this.map);
			var some=new SparkObj("Spark_0");
			
			some.setPosition(this.getPosition());
			some.setMap(this.map);
			this.map.addChild(some,this.EANIM);

			this.map.addStateObj(some);
		}
	},
	updateDir:function(dt){
		var def=this.curState.def;

		// 如果当前状态可以转身
		if(def.dir==1)
		{
			if(this.direction==1&&this.cmdMgr.hasCmd("bkw"))
			{
				this.direction=-1;
			}else if(this.direction==-1&&this.cmdMgr.hasCmd("fwd"))
			{
				this.direction=1;
			}
			
		}
	},
	updateMove:function(dt){
		this.updateDir(dt);
		this.updateSpeed(dt);
		

		//计算下一步位置
		var delx=0;
		var dely=0;
		if(this.speedx)
		{
			delx=this.speedx;
		}

		if(this.speedy)
		{
			dely=this.speedy;
		}

		var lastpos=this.getPosition();
		var nextpos=cc.p(lastpos._x+delx,lastpos._y+dely);

		//(this.direction*this.states.data.bound_width/2)
		var boundpos=cc.p(nextpos.x,
			lastpos._y)

		//计算能达到的位置,先x，后y
		var posRect=this.getBoundingRectByPos(boundpos);

		if(this.map.passableByRect(posRect)==0)
		{
		}
		else//不能通过
		{
			nextpos.x=lastpos._x;
		}

		posRect=this.getBoundingRectByPos(nextpos);
		if(this.map.passableByRect(posRect)==0)
		{
		}
		else//
		{
			this.speedy=0;
			nextpos.y=lastpos._y;
		}

		this.setPosition(nextpos);
	},
	//找到能移动到的最远位置
	findMaxPosX:function(myx,cantx){

	},
	updateSpeed:function(dt){
		this.speedy-=this.gravity;
	},
	updateTrigger:function(dt){
		var triggers=this.curState.trigger;
		for(var i=0;i<triggers.length;++i)
		{
			var temp=cc.clone(triggers[i])
			AdjustTriggerDir(temp,this);
			if(this.handleTrigger(temp))
				break;
		}
	},
	handleTrigger:function(trigger){

		//后面可以把处理函数弄成索引表？
		if(trigger.type=="changestate")
		{
			var bTrigger=false;

			var tTime=true
			if(trigger.animtime==0 || trigger.animtime)//根据帧数改变状态
			{

				if(this.getCurFrame()==trigger.animtime)
				{
					//this.changeAnim(trigger.state);
					tTime=true
				}else
				{
					tTime=false
				}
			}

			var tCmd=true
			if(trigger.command)
			{
				if(this.cmdMgr.hasCmd(trigger.command))
				{

				}else
				{
					tCmd=false;
				}
				
			}

			var trCmd=true
			if(trigger.rcommand)//不能有此命令
			{
				if(this.cmdMgr.hasCmd(trigger.rcommand))
				{
					trCmd=false
				}else
				{
					trCmd=true
				}
			}


			if(tCmd&&tTime&&trCmd)
			{
				this.changeState(trigger.state);
				return true
			}
				
		}else if(trigger.type=="velset")
		{
			this.speedx=trigger.x*this.direction;
			return false;
		}else if(trigger.type=="changedir")
		{
			var tCmd=true
			if(trigger.command)
			{
				if(this.cmdMgr.hasCmd(trigger.command))
				{
					trCmd=false
				}else
				{
					trCmd=true
				}
			}

			if(tCmd)
			{
				this.direction=this.direction==1?-1:1;
				this.cmdMgr.delCmd(trigger.command);
			}
		}
	},
	getCurFrame:function(){
		var anim=this.animList[this.curAnim];
		var nIndex=anim.getCurFrame();

		return nIndex;
	},getClsnds:function(){//获取当前的受击框
		var clsnds=this.animList[this.curAnim].getClsnds();
		if(clsnds)
		{
			if(this.direction==-1)
				return FlipRects(clsnds);
			else
				return clsnds;
		}else
		return null; 
	},
	//获取攻击框
	getClsnas:function(){
		var clsnds=this.animList[this.curAnim].getClsnas();
		if(clsnds)
		{
			if(this.direction==-1)
				return FlipRects(clsnds);
			else
				return clsnds;
		}else
		return null; 
	},
	//地图碰撞简化为统一用一个，与每一帧的打击框不同
	getBoundingRect:function(){
		//默认脚下为中心点，所以一般是负坐标

		if(this.boundRect==null)
		{
			var width2=this.states.data.bound_width/2;
			this.boundRect=cc.rect(-width2,this.states.data.bound_height,
				width2*2,this.states.data.bound_height);
		}

		return this.boundRect;
	},
	getBoundingRectByPos:function(ipos,bToWorld){
		var width2=this.states.data.bound_width/2;
		var rtRet=cc.rect(ipos.x-width2,ipos.y,
			width2*2,this.states.data.bound_height);

		if(bToWorld)
			rtRet=CmnUtils.rectToWorldRect(this,rtRet);

		return rtRet;
	}
});

